package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import servic.impl.CustomerManagerFactory;
import service.CustomerManager;

public class UpdateSubmitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerManager manager;
       
    public UpdateSubmitServlet() {
        super();
        manager=CustomerManagerFactory.getInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		System.out.println("id.........."+id);
		String cname = request.getParameter("cname");
		String csex = request.getParameter("sex");
		String csal = request.getParameter("sal");
		String cbirth = request.getParameter("birth");
		String[] cfavs = request.getParameterValues("favs");
				
		for(String f:cfavs){
			System.out.println(f);
		}
				
		//转换
		Date birth = null;
		int user_id = Integer.parseInt(id);
		boolean sex = Boolean.parseBoolean(csex);
		double sal = Double.parseDouble(csal);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			birth = sdf.parse(cbirth);
		} catch (ParseException e) {
			e.printStackTrace();
		}
				
		//封装
		Customer c = new Customer(user_id, cname, sal, birth, sex, cfavs);
		System.out.println(c.getId()+c.getCname()+c.getSal()+c.getBirth()+c.isSex()+c.getHobby());	
		
		int m=manager.updateCustomer(c);
		if(m>0){
			response.sendRedirect("list");
		}else{
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
		
		
	}

}
